package com.baomidou.springwind.util;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * Created by Administrator on 2016/1/27.
 */
public class FileUtil
{
    public String uploadFile(MultipartFile file, HttpServletRequest request) {
        String basePath = request.getSession().getServletContext().getRealPath("upload");
        String fileName = file.getOriginalFilename();
        File targetFile = new File(basePath, fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        //保存
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return request.getContextPath()+"/upload/"+fileName;
    }
}
